package org.hagen.mmia.yars.engine.corpora;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import org.hagen.mmia.yars.collection.Collection;
import org.hagen.mmia.yars.collection.Document;
import org.hagen.mmia.yars.collection.Topic;

/*
 * very simple parser
 */
public class ClefIP extends Reader {

	private static boolean verbose = false;		
	
	SAXParserFactory _spf = null;
	SAXParser  _saxParser = null;
	XMLReader  _xmlReader = null;

	public ClefIP() throws ParserConfigurationException, SAXException {

		_spf = SAXParserFactory.newInstance();
		_spf.setNamespaceAware(true);
		
		_saxParser = _spf.newSAXParser();
		_xmlReader = _saxParser.getXMLReader();
	}

	public void readAbstract( ) {
	}

	private static String convertToFileURL(String filename) {
		
        String path = new File(filename).getAbsolutePath();
        if (File.separatorChar != '/') {
            path = path.replace(File.separatorChar, '/');
        }

        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return "file:" + path;
    }	
	
	public ClefIPHandler readAbstract( String filename ) throws IOException, SAXException {

		ClefIPHandler handler = new ClefIPHandler();
		_xmlReader.setContentHandler(handler);
		_xmlReader.parse(convertToFileURL(filename));
		  
		// print patent
		if( verbose ) {
		  System.out.println( "ID: " + handler.patentNumber );
		  System.out.println( "Title: " + handler.title.toLowerCase() );
		  if( handler.content != null )
			  System.out.println( "Content: " + handler.content.toLowerCase() );
		  if(  handler.description != null )
			  System.out.println( "Description: " + handler.description.toLowerCase() );

		  System.out.print("\n");
		}
		
		return handler;
	}

	public void readTopic( ) {
	}

	public void readRelevance( ) {

		String filenameXL = "C:\\DATA\\ClefIP\\CLEFIP09-QRELS-XL.txt";
		readRelevance( filenameXL );
	}
	
	public void readRelevance( String filename ) {

		File file = new File(filename);

		try {
			Scanner in = new Scanner(file);

			Topic	 topic = null;
			int		 index = 0;
			String	prevID = new String( " " );
			
			while (in.hasNextLine()) {

				String  topicID = null;
				String patentID = null;

				String line = in.nextLine();
				String[] elements = line.split( " " );
				if( elements != null ) {
					if( elements.length != 4 ) {
						continue;
					}
				}
				
				topicID  = elements[0];
				patentID = elements[2];

				if( !prevID.equals( topicID ) ) {
					topic = new Topic( index, topicID );
					topic.setNeed( "find prior art for patent " + topicID );
					Collection.addTopic(topic);
					index++;
				}
				
				Document doc = Collection.getDocument( patentID );
				if( doc == null ) {
					//System.err.println( "document " + patentID + " not in index");
					continue;
				}
				if( doc.ID == null || doc.getContent() == null || doc.getTitle() == null ) {
					System.err.println( "document " + patentID + " is empty");
					continue;
				}
				
				topic.relSet.add( patentID );
				
				prevID = topicID;
			}

			in.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
	}
}


